package com.hankol.message.fragment

import android.os.Bundle
import com.baselibrary.MessageBus
import com.baselibrary.extension.checkNotNull
import com.baselibrary.utils.EventBusUtils
import io.rong.imkit.conversationlist.ConversationListFragment
import io.rong.imkit.widget.refresh.api.RefreshLayout
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
//自定义 会话列表
class MyConversationListFragment : ConversationListFragment() {
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
    }

    override fun onConversationListRefresh(refreshLayout: RefreshLayout?) {
        super.onConversationListRefresh(refreshLayout)
        //刷新会话列表
        EventBusUtils.sendNotify(MessageBus.msgid_refresh_chat_list)
    }
    override fun onDestroyView() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
        super.onDestroyView()
    }

    //EventBus 接受方法
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEvent(messageBus: MessageBus) {
        if (messageBus.codeType == MessageBus.msgid_exit_or_logout||messageBus.codeType == MessageBus.msgId_login_up) { //登录&退出&注销账号
            if (mConversationListViewModel.getConversationListLiveData().getValue().checkNotNull()){
                mConversationListViewModel.getConversationListLiveData().getValue()?.clear();
            }
            mConversationListViewModel.getConversationList(false, true,0L);
        }
    }

}